<?php
/*
+---------------------------------------------------------------------------
|   PHP-IRC v2.2.0
|   ========================================================
|   by Manick
|   (c) 2001-2005 by http://phpbots.sf.net/
|   Contact: manick@manekian.com
|   irc: #manekian@irc.rizon.net
|   ========================================
+---------------------------------------------------------------------------
|   > Seen Mod
|   > Module written by Manick
|   > Module Version Number: 0.1
+---------------------------------------------------------------------------
|   > This program is free software; you can redistribute it and/or
|   > modify it under the terms of the GNU General Public License
|   > as published by the Free Software Foundation; either version 2
|   > of the License, or (at your option) any later version.
|   >
|   > This program is distributed in the hope that it will be useful,
|   > but WITHOUT ANY WARRANTY; without even the implied warranty of
|   > MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|   > GNU General Public License for more details.
|   >
|   > You should have received a copy of the GNU General Public License
|   > along with this program; if not, write to the Free Software
|   > Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+---------------------------------------------------------------------------
|   Changes
|   =======-------
|   > If you wish to suggest or submit an update/change to the source
|   > code, email me at manick@manekian.com with the change, and I
|   > will look to adding it in as soon as I can.
+---------------------------------------------------------------------------
*/

class seen_mod extends module {

	public $title = "Seen Mod";
	public $author = "Manick";
	public $version = "0.1";
	
	private $seen;


	// Update actions
	public function seen($line, $args)
	{
		
		if($line['to'] == $this->ircClass->getNick())
		{
			//manjary hita ny pv @ botlives
			return;
		}
		if (strtolower($line['cmd']) == "join")
		{
			$line['text'] = "";
		}

		if (strtolower($line['cmd']) == "kick")
		{
			$offsetA = strpos($line['params'], chr(32));

			$act = "kick";
			$user = substr($line['params'], $offsetA + 1);
			
			$this->addLast($user, $act, $line['text']);

		}
		else
		{
			$this->addLast($line['fromNick'], strtolower($line['cmd']), $line['text']);
		}
	}

	private function getLast($user)
	{
	
		$query = $this->db->query("SELECT * FROM ci_karajia_seen WHERE user='[1]' LIMIT 1", $user);
		
		if ($this->db->numRows($query))
		{
			return $this->db->fetchArray($query);
		}
		else
		{
			return false;
		}
	}

	private function addLast($user, $act, $txt)
	{
		$act = irc::myStrToLower($act) ;
		$this->db->query("INSERT INTO ci_karajia_seen (user, dt, act, txt) VALUES ('{$user}','" . time() . "', '{$act}', '[1]')  ON DUPLICATE KEY UPDATE dt = '" . time() . "', act='{$act}', txt='[1]'", $txt);
	}

	// User interface
	public function priv_seen($line, $args)
	{

		if ($line['to'] == $this->ircClass->getNick())
		{
			return;
		}

		if ($args['nargs'] <= 0)
		{
			$this->ircClass->notice($line['fromNick'], "Fampiasana azy: !seen <nick>");
			return;
		}

		$user = irc::myStrToLower($args['arg1']);

		if ($user == irc::myStrToLower($line['fromNick']))
		{
			$this->ircClass->privMsg($line['to'], $line['fromNick'] . ", loll... mijery tena?...");
			return;
		}

		$data = $this->getLast($user);

		if ($data === false)
		{
			$this->ircClass->privMsg($line['to'], $line['fromNick'] . ", Mbola tsy nandalo teto mihitsy i " . $args['arg1']);
			return;
		}

		$time = time() - $data['dt'];
		
		if ($time > 3600*24)
		{
			$timeString = irc::timeFormat($time, "%d andro sy %h ora sy %m minitra sy %s segondra");
		}
		else if ($time > 3600)
		{
			$timeString = irc::timeFormat($time, "%h ora sy %m minitra %s segondra");
		}
		else if ($time > 60)
		{
			$timeString = irc::timeFormat($time, "%m minitra %s segondra");
		}
		else
		{
			$timeString = irc::timeFormat($time, "%s segondra");
		}

		$action = "";

		switch ($data['act'])
		{
			case "privmsg":
				$action = "niresaka teto amin'ny efitra";
				break;
			case "notice":
				$action = "nandefa notice";
				break;
			case "join":
				$action = "niditra efitra";
				break;
			case "kick":
				$action = "voadaka";
				break;
			case "part":
				$action = "nivoaka ny efitra";
				break;
			case "quit":
				$action = "niala";
				break;
		}

		if ($data['txt'] != "")
		{
			$action .= ": " . $data['txt'];
		}

		$this->ircClass->privMsg($line['to'], $line['fromNick'] . ", Hitako farany i " . BOLD . $data['user'] . NOFORMAT . BLUE . " " . $timeString . NOFORMAT . " lasa teo (" . $action . ").");

	}
}

?>
